<script setup lang="ts">
/**
 * 系统管理-系统设置-权限管理
 */
import {onMounted, ref} from 'vue'
import {deletePermissionApi, getPermissionPageApi} from '@/assets/api/systemManagement/system/permission'
import {Refresh, Search} from '@element-plus/icons-vue'
import PermissionEdit from '@/views/systemManagement/system/permission/PermissionEdit.vue'
import {ElMessage} from 'element-plus'
import TableToolButton from '@/components/base/TableToolButton.vue'

// 查询条件
const form = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  permissionDesc: '',
  menuNames: '',
  menuSubNames: ''
})

const onSubmit = () => {
  form.value.pageNum = 1
  getTableData()
}

const onReset = () => {
  form.value.permissionDesc = ''
  form.value.menuNames = ''
}

// 全选按钮
const multipleTableRef = ref()
const multipleSelection = ref<object[]>([])
const handleSelectionChange = (val: object[]) => {
  multipleSelection.value = val
}

// 获取表格数据
const tableData = ref([])
const tableLoading = ref(false)
const getTableData = () => {
  getPermissionPageApi(form.value).then(res => {
    tableLoading.value = false
    if (res.data.code == 200) {
      tableLoading.value = false
      tableData.value = res.data.data.records
      form.value.total = res.data.data.total
    } else
      ElMessage.error({message: res.data.data, showClose: true})
  })
}

// 页码切换处理器
const handleCurrentChange = () => {
  getTableData()
}

// 新增、编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editForm = ref() // 编辑的数据
const echoMenu = ref() // 回显的菜单数据
const echoSubMenu = ref() // 回显的菜单子表数据
const openDialog = (tag: string, obj: object) => {
  dialogTitle.value = tag
  editForm.value = obj
  // 回显菜单
  echoMenu.value = []
  if (tag === '编辑') {
    if (editForm.value.menuNames != null) {
      editForm.value.menuNames.split(',').forEach((item: string) => {
        echoMenu.value.push(item)
      })
    }
    echoSubMenu.value = []
    if (editForm.value.menuSubNames != null) {
      editForm.value.menuSubNames.split(',').forEach((item: string) => {
        echoSubMenu.value.push(item)
      })
    }
  } else {
    editForm.value.isParent = 2
  }
  dialogVisible.value = true
}

// 编辑修改回调
const saveCallBack = (saved: boolean) => {
  if (saved) {
    dialogVisible.value = false
    getTableData()
  }
}

// 删除
const deleteCallBack = (ids: string) => {
  deletePermissionApi(ids).then(res => {
    if (res.data.code === 200) {
      ElMessage.success({message: res.data.data, showClose: true})
      getTableData()
    } else {
      ElMessage.error({message: res.data.data, showClose: true})
    }
  })
}

onMounted(() => {
  getTableData()
})
</script>

<template>
  <el-card shadow="never">
    <template #header>
      <el-breadcrumb>
        <el-breadcrumb-item>系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      </el-breadcrumb>
    </template>
    <!-- 搜索 -->
    <el-row style="padding: 0.4%;">
      <el-col :span="24">
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="权限描述">
            <el-input v-model="form.permissionDesc" style="width: 200px" placeholder="请输入权限描述" clearable/>
          </el-form-item>
          <el-form-item label="所属菜单">
            <el-input v-model="form.menuNames" style="width: 200px" placeholder="请输入菜单名称" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :icon="Search">搜索</el-button>
            <el-button @click="onReset" :icon="Refresh">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 表格工具 -->
    <el-row style="padding: 0.4%;">
      <el-col :span="24">
        <table-tool-button :export-visible="false" :selected-array="multipleSelection" @to-add="openDialog"
                           @to-edit="openDialog" @to-delete="deleteCallBack"/>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row style="padding: 0.4%;">
      <el-col :span="24">
        <el-table
          ref="multipleTableRef"
          v-loading="tableLoading"
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          stripe
          border
        >
          <el-table-column type="selection" width="55"/>
          <el-table-column property="permissionName" label="权限名称"/>
          <el-table-column property="menuNames" label="所属菜单" width="200"/>
          <el-table-column property="menuSubNames" label="菜单子表" width="200"/>
          <el-table-column property="permissionDesc" label="权限描述"/>
          <el-table-column property="isParent" label="分类" width="100">
            <template #default="scope">
              <span v-if="scope.row.isParent === 1">父级</span>
              <span v-if="scope.row.isParent === 0">子级</span>
              <span v-else>操作</span>
            </template>
          </el-table-column>
          <el-table-column property="remark" label="备注" width="200"/>
        </el-table>
        <el-pagination :page-size="form.pageSize" v-model:current-page="form.pageNum"
                       @current-change="handleCurrentChange" layout="prev, pager, next" v-model:total="form.total"/>
      </el-col>
    </el-row>
    <!--  新增、编辑弹窗 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600"
      :destroy-on-close="true"
    >
      <permission-edit :form="editForm" :echo-menu="echoMenu" :echo-menu-sub="echoSubMenu"
                       @close="dialogVisible = false"
                       @save="saveCallBack" disabled></permission-edit>
    </el-dialog>
  </el-card>
</template>

<style scoped>

</style>
